package com.xx.xiawan.net;

import android.content.Context;
import android.content.res.Resources;

/**
 * Created by Administrator on 2017-08-23.
 */

public enum ErrorCode {
    UNKNOWN_ERROR(-10000, -1, "未知错误");
    private final int mErrorCode;
    private final int mResId;
    private final String mErrorMessage;
    ErrorCode(int errorCode, int resId, String message) {
        this.mErrorCode = errorCode;
        this.mResId = resId;
        this.mErrorMessage = message;
    }

    public static ErrorCode codeOf(int code) {
        for (ErrorCode errorCode : values()) {
            if (errorCode.mErrorCode == code) {
                return errorCode;
            }
        }
        return UNKNOWN_ERROR;
    }

    public int getCode() {
        return mErrorCode;
    }

    public String getMessage(Context context) {
        if (context != null && mResId != 0) {
            Resources res = context.getResources();
            if (res != null) {
                return context.getResources().getString(mResId);
            }
        }
        return mErrorMessage;
    }
}
